var app = new Vue({
  el: '#app',
  data: {
    currentMonthDays: '', // 获取当月天数
    currentWeekDay: '', // 获取当前月的1号是星期几
    lastMonthDays: '', // 获取上月天数
    selectedMonthDate: moment(new Date).format('YYYY-MM-DD'), // 当前时间
    curMonthArr: [], // 自定义数组 -- 当月
    ticketArr: [ // 把获取到的数据放在这个数组里
      {
        day: '2019-06-28',
        price: '30'
      },
      {
        day: '2019-06-29',
        price: '30'
      },
      {
        day: '2019-06-30',
        price: '30'
      },
      {
        day: '2019-07-20',
        price: '60'
      },
    ]
  },
  methods: {
    // 获取当月日期数据
    getCurMonthData (getData) {
      // 获取当前月的1号是星期几
      this.currentWeekDay = moment(getData).date(1).weekday();
      // console.log(this.currentWeekDay);
      this.lastMonthDays = moment(getData).subtract(1, 'month').daysInMonth();
      // console.log(this.lastMonthDays);
      // 获取当月天数
      this.currentMonthDays = moment(getData).daysInMonth();
      // 总天数是31，1号是 周五 或者 周六 -- 6个[] 或者 总天数是30，1号是 周六 -- 6个[]
      if ((this.currentMonthDays == '31' && this.currentWeekDay == '5' || this.currentWeekDay == '6' ) || (this.currentMonthDays == '30' && this.currentWeekDay == '6')) {
        var daysArr = [[], [], [], [], [], []];
      } else {
        var daysArr = [[], [], [], [], []];
      }
      // 日期处理
      // const getDay = day => (day <= this.lastMonthDays ? '' : (day <= (this.lastMonthDays + this.currentMonthDays)) ? day - this.lastMonthDays : '');

      var _this = this;
      var getDay = function getDay(day) {
        return day <= _this.lastMonthDays ? '' : day <= _this.lastMonthDays + _this.currentMonthDays ? day - _this.lastMonthDays : '';
      };

      for (var i = 0; i < 7; i++) {
        var virtualDay = (this.lastMonthDays - this.currentWeekDay) + i + 1;
        for (var j = 0; j < daysArr.length; j += 1) {
          daysArr[j][i] = {'day' : getDay(virtualDay + (j * 7))};
        }
      }
      // console.table(daysArr);
      this.curMonthArr = daysArr;
    },

    // 封装检索函数 -- 这一天是否有售票
    SearchTicket (selectedMonthDate) {
      for (var i = 0; i < this.curMonthArr.length; i++) {
        // console.log(this.curMonthArr[i])
        for (var j = 0; j < this.curMonthArr[i].length; j++) {
          for (var k = 0; k < this.ticketArr.length; k++) {
            if (this.ticketArr[k].day.split('-')[0] == selectedMonthDate.split('-')[0]) {
              if (this.ticketArr[k].day.split('-')[1] == selectedMonthDate.split('-')[1]) {
                if (this.ticketArr[k].day.split('-')[2] == this.curMonthArr[i][j].day) {
                  // console.log(this.curMonthArr[i][j].day);
                  this.curMonthArr[i][j].price = '￥' + this.ticketArr[k].price;
                  this.curMonthArr[i][j].state = '票';
                }
                // 判断当前日期 --- 添加标记
                if (this.curMonthArr[i][j].day == this.selectedMonthDate.split('-')[2]) {
                  // console.log(this.curMonthArr[i][j].day)
                  this.curMonthArr[i][j].cur = '今天';
                }
              }
            }
          }
        }
      }
    },
    // 获取被选中的这个月
    getMonth (state) {
      if (state == 'prev') {
        this.selectedMonthDate = moment(this.selectedMonthDate).subtract(1, 'month').format('YYYY-MM-DD');
      } else if (state == 'next') {
        this.selectedMonthDate = moment(this.selectedMonthDate).add(1, 'month').format('YYYY-MM-DD');
      }
      this.getCurMonthData(this.selectedMonthDate);
      this.SearchTicket(this.selectedMonthDate);
      console.log(this.selectedMonthDate)
    },
    // 获取上一个月
    prevMonthBtn () {
      this.getMonth('prev');
    },
    // 获取下一个月
    nextMonthBtn () {
      this.getMonth('next');
    },
    // 被选中日期的点击事件
    selectedDaysClickEvent (e) {
      var dayInnerText = e.currentTarget.querySelector('.day').innerHTML;
      // console.log(dayInnerText)
      if (dayInnerText == '') {
        // 选中的日期是空日期
        // console.log('空')
      } else if (!e.currentTarget.querySelector('.state')) {
        this.$toast('该日期暂无售票');
      } else if (e.currentTarget.querySelector('.state')) {
        // console.log('有票')

        // --- 在这里写事件(这里指的是有票的栏目被点击后触发的事件)
        
      }
    }
  },
  mounted () {
    this.getCurMonthData(this.selectedMonthDate);
    this.SearchTicket(this.selectedMonthDate);
  }
})
